package com.lin.views;



import java.util.ArrayList;


import com.lin.utils.Parameters;
import com.lin.utils.Utils;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
import com.yunfeng.supercall.R;
import com.yunfeng.supercall.bean.SuperFriendsBean;


import android.content.ContentResolver;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.TextureView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

public class ListViewForGuanzhu extends ListView{


	private int TYPE_ITEM = 0;
	private int TYPE_FRIENDS = 1;

	MyAdapter mFocusGameAdapter;
	Context mContext;
	DisplayImageOptions options;
	public ListViewForGuanzhu(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub

		mContext = context;
		options = new DisplayImageOptions.Builder()
		.displayer(new RoundedBitmapDisplayer(context.getResources().getDimensionPixelOffset(R.dimen.corners_avatar)))
		.showImageOnFail(R.drawable.default_avatar)
		.showImageOnLoading(R.drawable.default_avatar)
		.cacheInMemory(true)
		.cacheOnDisk(true)
		.considerExifParams(true)
		.bitmapConfig(Bitmap.Config.RGB_565)
		.build();

	}
	
	public void updateView() {
		mFocusGameAdapter.notifyDataSetChanged();
	}
	


	public void setDataList(ArrayList<SuperFriendsBean> mFocusGameList) {
		if (mFocusGameAdapter == null) {
			mFocusGameAdapter = new MyAdapter(getContext(), mFocusGameList);
			setAdapter(mFocusGameAdapter);
		} else {
			mFocusGameAdapter.setmFocusGameList(mFocusGameList);
			mFocusGameAdapter.notifyDataSetChanged();
		}
	}

	private class MyAdapter extends BaseAdapter {

		LayoutInflater mLayoutInflater;
		ArrayList<SuperFriendsBean> mDataList;
		int mSize;




		public MyAdapter(Context context,
				ArrayList<SuperFriendsBean> mDataList) {
			// TODO Auto-generated constructor stub
			this.mDataList = mDataList;
			mLayoutInflater =  LayoutInflater.from(mContext);
		}

		public ArrayList<SuperFriendsBean> getDataList() {
			return mDataList;
		}

		public void setmFocusGameList(ArrayList<SuperFriendsBean> mFocusGameList) {
			this.mDataList = mFocusGameList;
		}


		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			mSize = mDataList.size();
			return mSize;
		}


		@Override
		public long getItemId(int arg0) {
			// TODO Auto-generated method stub
			return arg0;
		}



		public int getViewTypeCount() {
			// TODO Auto-generated method stub
			return 2;
		}



		@Override
		public int getItemViewType(int position) {
			// TODO Auto-generated method stub
			if(position == 0 || position == 1 || position == 2 || position == 3 || position == 4) {
				return TYPE_ITEM;
			} else {
				return TYPE_FRIENDS;
			}
		}


		@Override
		public SuperFriendsBean getItem(int position) {
			// TODO Auto-generated method stub
			return mDataList.get(position);
		}

		@Override
		public View getView(int position, View convertView, ViewGroup arg2) {
			// TODO Auto-generated method stub
			ViewHolder viewholder = null;

			int type = getItemViewType(position);
			if (convertView == null) {
				viewholder = new ViewHolder();
				if( type == TYPE_ITEM) {
					convertView = mLayoutInflater.inflate(
							R.layout.list_item_super_friends, null);
				} else if( type == TYPE_FRIENDS) {
					convertView = mLayoutInflater.inflate(
							R.layout.list_item_super_friends, null);
				}

				viewholder.iv = (ImageView) convertView.findViewById(R.id.iv);
				viewholder.tv = (TextView) convertView.findViewById(R.id.tv);
				viewholder.tvLevel = (TextView) convertView.findViewById(R.id.level);
				convertView.setTag(viewholder);

			} else {
				viewholder = (ViewHolder) convertView.getTag();
			}
			
			

			setDivider(null);
			if(position < 5) {
				if(position == 0)
					viewholder.tv.setText("新的超友");
				else if(position == 1) {
					viewholder.tv.setText("超有分组");	
				}else if(position == 2) {
					viewholder.tv.setText("我的圈子");
				}else if(position == 3) {
					viewholder.tv.setText("企业超号");
				}else if(position == 4) {
					viewholder.tv.setText("");
				}

			} else {
				SuperFriendsBean base = getItem(position - 5);

				viewholder.tv.setText(base.getNick_name());
				setLevelColor(base.getLevel(), viewholder.tv, viewholder.tvLevel);
				//			iv.setImageURI(Utils.getURI(mContext, R.drawable.ic_new_super_friend));
				//			String url = Utils.getURI(mContext, R.drawable.ic_new_super_friend).toString();
				ImageLoader.getInstance().displayImage(Parameters.SERVER_PIC_URL + base.getPhoto(), viewholder.iv, options);
			}

			return convertView;

			/*SuperFriendsBean.setupSingleLineContactsView(convertView, base);
			if (getItemViewType(position) == SuperFriendsBean.TYPE_SYSTEM_CONTACTS) {
				View red = ViewHolder.get(convertView, R.id.red_dot);
				if (position == 0) {
					boolean has = SuperFriendsManager.hasNewFriend(getContext());
					red.setVisibility(has ? View.VISIBLE : View.INVISIBLE);
				} else {
					red.setVisibility(View.INVISIBLE);
				}
			}*/
		}



		void setLevelColor(int level,TextView tv, TextView tvLevel) {
			try {

				if(level == 0) {
					tvLevel.setTextColor(getResources().getColor(R.color.textview_title_listview));
					tv.setTextColor(getResources().getColor(R.color.textview_title_listview));	
				} else if(level == 1) {
					tvLevel.setTextColor(getResources().getColor(R.color.level_or));
					tv.setTextColor(getResources().getColor(R.color.level_or));
				} else {
					tvLevel.setTextColor(getResources().getColor(R.color.level_red));
					tv.setTextColor(getResources().getColor(R.color.level_red));
				}
			} catch(NullPointerException e) {

			}
		}
	}

	static class ViewHolder {
		ImageView iv;
		TextView tv;
		TextView tvLevel;
	}
}